package cn.dt70.controller.app;

import cn.dt70.pojo.AppCategory;
import cn.dt70.pojo.AppInfo;
import cn.dt70.pojo.AppVersion;
import cn.dt70.pojo.DataDictionary;
import cn.dt70.services.AppCategoryService;
import cn.dt70.services.AppInfoService;
import cn.dt70.services.DataDictionaryService;
import cn.dt70.services.VersionService;
import cn.dt70.utils.PageHelper;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Controller
@RequestMapping("/dev/")
public class AppController {
    @Resource
    private AppInfoService appInfoService;
    @Resource
    private DataDictionaryService dataDictionaryService;
    @Resource
    private AppCategoryService appCategoryService;
    @Resource
    private  VersionService versionService;
//查看app列表
    @RequestMapping("applist")
    public String qryAppList(PageHelper ph, Model model,
                             @RequestParam(value = "querySoftwareName",required = false)String querySoftwareName,
                             @RequestParam(value = "queryStatus",required = false)String queryStatus,
                             @RequestParam(value = "queryFlatformId",required = false)String queryFlatformId,
                             @RequestParam(value = "queryCategoryLevel1",required = false)String queryCategoryLevel1,
                             @RequestParam(value = "queryCategoryLevel2",required = false)String queryCategoryLevel2,
                             @RequestParam(value = "queryCategoryLevel3",required = false)String queryCategoryLevel3){
        if (null == queryStatus){
            queryStatus="0";
        }
        if(null == queryFlatformId){
            queryFlatformId="0";
        }
        if (null == queryCategoryLevel1 ||queryCategoryLevel1.equals( "" )){
            queryCategoryLevel1 ="0";
        }
        if (null == queryCategoryLevel2 ||queryCategoryLevel2.equals( "" )){
            queryCategoryLevel2 = "0";
        }
        if (null == queryCategoryLevel3 ||queryCategoryLevel3.equals( "" )){
            queryCategoryLevel3 = "0";
        }
        List<AppInfo> appInfoList = appInfoService.qryAllAPP(ph,querySoftwareName,Integer.parseInt( queryStatus ),Integer.parseInt( queryFlatformId),Integer.parseInt(queryCategoryLevel1),Integer.parseInt(queryCategoryLevel2 ),Integer.parseInt(queryCategoryLevel3 ));
        List<DataDictionary> statusList = dataDictionaryService.qryStatus();
        List<DataDictionary> flateFormList = dataDictionaryService.qryFlatform();
        List<AppCategory> appCategory1List = appCategoryService.qryAppCategory1();
        List<AppCategory> appCategory2List = appCategoryService.qryAppCategory2(Integer.parseInt( queryCategoryLevel1 ));
        List<AppCategory> appCategory3List = appCategoryService.qryAppCategory3(Integer.parseInt( queryCategoryLevel2 ));

        model.addAttribute( "flateFormList",flateFormList );
        model.addAttribute( "appInfoList",appInfoList );
        model.addAttribute( "statusList",statusList );
        model.addAttribute( "appCategory1List",appCategory1List );
        model.addAttribute( "appCategory2List",appCategory2List );
        model.addAttribute( "appCategory3List",appCategory3List );

        model.addAttribute("currentPageNo",ph.getCurrentPageNo());
        model.addAttribute("totalCount",ph.getTotalCount());
        model.addAttribute("totalPageCount",ph.getTotalPageCount());

        model.addAttribute( "querySoftwareName",querySoftwareName );
        model.addAttribute( "queryStatus",queryStatus );
        model.addAttribute( "queryFlatformId",queryFlatformId );
        model.addAttribute( "queryCategoryLevel1",queryCategoryLevel1 );
        model.addAttribute( "queryCategoryLevel2",queryCategoryLevel2 );
        model.addAttribute( "queryCategoryLevel3",queryCategoryLevel3 );
        return "developer/appinfolist";
    }
//二级分类
    @RequestMapping(value = "categorylevel2",produces = "application/json;charset=utf-8")
    @ResponseBody
    public String qryCategorylevel2(@RequestParam("pid")String pid, Model model){
        List<AppCategory> appCategoryList2 = appCategoryService.qryAppCategory2( Integer.parseInt( pid ) );
        Gson gson = new Gson();
        System.out.println(gson.toJson( appCategoryList2 ));
        return gson.toJson( appCategoryList2 );
    }
//三级分类
@RequestMapping(value = "categorylevel3",produces = "application/json;charset=utf-8")
@ResponseBody
    public String qryCategorylevel3(@RequestParam("pid")String pid){
        List<AppCategory> appCategoryList3 = appCategoryService.qryAppCategory3( Integer.parseInt( pid ) );
        Gson gson = new Gson();
        return gson.toJson( appCategoryList3 );
}

    //查看App信息
    @RequestMapping(value = "/appview/{id}")
    public String view(@PathVariable String id, Model model) {
        AppInfo appInfo = null;
        appInfo = appInfoService.getAppInfo(Integer.parseInt(id));

        List<AppVersion> versionById = versionService.findVersionById(Long.parseLong(id));

        model.addAttribute("appInfo",appInfo);
        model.addAttribute("appVersionList",versionById);
        return "developer/appinfoview";
    }

    //修改appinfo表的状态status（进行上架，下架）
    @RequestMapping(value = "updInfoStatusById",produces = "application/json;charset=utf-8")
    @ResponseBody
    public String uppInfoStatusById(@RequestParam("appId") String id,
                                    @RequestParam("appStatus") String status,
                                    Model model){
        AppInfo appInfo = null;
        appInfo = appInfoService.updateAppInfoById(Long.parseLong(id),Long.parseLong(status));
        if (null == appInfo){
            System.out.println("修改状态status失败");
        }else {
            System.out.println("修改状态status成功");
        }

        return "forward:/applist";
    }

    @RequestMapping("del")
    @ResponseBody
    public String delAppInfoById(@RequestParam("id")int appId,Model model){
        Gson gson = new Gson();
        if(appInfoService.removerAppInfo(appId)){
            System.out.println("删除成功！");
            String str = "{delResult:true}";
            return gson.toJson(str);
        }else{
            System.out.println("删除失败！");
        }
        return "developer/appinfolist";
    }
}
